<?php

Yii::import('application.vendors.*');
require_once('phpthumb/ThumbLib.inc.php');

class ImageProcessor
{
    public static function process($imageFile, $operations, $options=array())
    {
        try {
            $thumb = PhpThumbFactory::create($imageFile, $options);
            foreach($operations as $operation) {
                $op=$operation[0];
                self::$op($thumb, $operation);
            }
            return true;
        }
        catch (Exception $e) {
             Yii::log("catch exception thrown from phpThumb. errmsg=".$e->getMessage()." imageFile=$imageFile",CLogger::LEVEL_ERROR,'application.imageProcessor');
             return false;
        }
    }  
    
    private static function adaptiveResize(&$thumb, $params)
    {
        $thumb->adaptiveResize($params['width'], $params['height']);
    }

    private static function crop(&$thumb, $params)
    {
        $thumb->crop($params['startX'], $params['startY'], $params['cropWidth'], $params['cropHeight']); 
    }

    private static function cropFromCenter(&$thumb, $params)
    {
        //$params['cropHeight'] can be null
        $thumb->cropFromCenter($params['cropWidth'], $params['cropHeight']); 
    }
    
    private static function resize(&$thumb, $params)
    {
        $thumb->resize(
            isset($params['maxWidth'])?$params['maxWidth']:0, 
            isset($params['maxHeight'])?$params['maxHeight']:0
        ); 
    }

    private static function resizePercent(&$thumb, $params)
    {
        $thumb->resizePercent(isset($params['percent'])?$params['percent']:0); 
    }
    
    private static function rotateImage(&$thumb, $params)
    {
        $thumb->rotateImage(isset($params['direction'])?$params['direction']:'CW'); 
    }

    private static function rotateImageNDegrees(&$thumb, $params)
    {
        $thumb->rotateImageNDegrees($params['degrees']); 
    }
    
    private static function save(&$thumb, $params)
    {
        $thumb->save($params['fileName'], $params['format']); 
    }
    
    private static function show(&$thumb, $params)
    {
        $thumb->show();
    }
}
?>
